package com.dfec.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.model.User;
import com.dfec.service.IUserService;
import com.dfec.service.imp.UserServiceImp;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;

/**
 * Servlet implementation class UpdateInfoServlet
 */
@WebServlet(name = "updateInfo", description = "修改用户资料", urlPatterns = { "/updateInfo" })
public class UpdateInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IUserService is=new UserServiceImp();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Object> list =new ArrayList<>();
		User us=new User();
		us.setUser_name(request.getParameter("user_name"));
		us.setEmail(request.getParameter("email"));
		us.setPhone(request.getParameter("phone"));
		us.setUser_pass(request.getParameter("user_pass"));
		us.setHead_icon(request.getParameter("head_icon"));
		us.setQq(request.getParameter("qq"));
		String id = request.getParameter("id");
		us.setId(null!=id?Integer.parseInt(id):0);
		Msg msg=new Msg();
		User user=new User();
		try {
			boolean i = is.updateUser(us);
			if(i) {
				  msg.setCode(Consists.Error_Code_OK);
				  msg.setMsg(Consists.Error_Msg_HANDLER_OK);
				  user=is.getUserById(us);
			  }else {
				  msg.setCode(Consists.Error_Code_FAIL);
				  msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
			  }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		  list.add(msg);
		  list.add(user);
		  ResponseUtil.sendScript(response, JSON.toJSONString(list));
	}

}
